﻿using System;
using System.Collections.Generic;
using System.Text;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Faceid.V20180301;
using TencentCloud.Faceid.V20180301.Models;
using Infrastructure.Error;

namespace OtherLib.TenXun
{
    public static class IDVaildUtil
    {
        private static FaceidClient client;
        static IDVaildUtil()
        {
            var clientProfile = new ClientProfile();
            var httpProfile = new HttpProfile();
            httpProfile.Endpoint = ("faceid.tencentcloudapi.com");
            clientProfile.HttpProfile = httpProfile;
            client = new FaceidClient(TenXunConfig.Cred, "ap-beijing", clientProfile);
        }
        /// <summary>
        /// 活体人脸核身
        /// </summary>
        /// <param name="IdCard"></param>
        /// <param name="Name"></param>
        /// <param name="VideoBase64"></param>
        /// <param name="LivenessType"></param>
        /// <returns>相似度 100</returns>
        public static float ValidAlive(string IdCard, string Name, string VideoBase64, string LivenessType = "ACTION")
        {
            try
            {
                LivenessRecognitionRequest req = new LivenessRecognitionRequest();
                req.IdCard = IdCard;
                req.Name = Name;
                req.VideoBase64 = VideoBase64;
                req.LivenessType = LivenessType;
                LivenessRecognitionResponse resp = client.LivenessRecognitionSync(req);
                Console.WriteLine(AbstractModel.ToJsonString(resp));

                if ("Success".Equals(resp.Result)) return resp.Sim ?? 0;

                throw new BizException(resp.Description);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw e;
            }
        }

        /// <summary>
        /// 身份信息认证
        /// </summary>
        /// <param name="IdCard"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static bool ValidCard(string IdCard, string Name)
        {
            try
            {
                IdCardVerificationRequest req = new IdCardVerificationRequest();
                req.IdCard = IdCard;
                req.Name = Name;

                IdCardVerificationResponse resp = client.IdCardVerificationSync(req);
                Console.WriteLine(AbstractModel.ToJsonString(resp));

                if ("0".Equals(resp.Result)) return true;

                throw new BizException(resp.Description);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                throw e;
            }
        }
    }
}
